package com.sitanyun.merchant.guide.adapter;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.SparseArray;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.bean.ProductBean;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by jzl
 * on 2022/3/23.
 * 产品许可
 */
public class ProductAdapter extends BaseQuickAdapter<ProductBean.DataBean.SceneListBean.RecordsBean, BaseViewHolder> {
    private  Date one;
    private  Date two;
    private  long day = 0;
    private long hour = 0;
    private  long min = 0;
    private  long sec = 0;
    private String days;
    private CountDownTimer countDownTimer;
    private SparseArray<CountDownTimer> countDownMap;
    public ProductAdapter(Context context) {
        super(R.layout.product_item);
        this.mContext = context;
        this.countDownMap = new SparseArray<>();
    }
    /**
     * 清空资源
     */
    public void cancelAllTimers() {
        if (countDownMap == null) {
            return;
        }
//        Log.e("TAG",  "size :  " + countDownMap.size());
        for (int i = 0,length = countDownMap.size(); i < length; i++) {
            CountDownTimer cdt = countDownMap.get(countDownMap.keyAt(i));
            if (cdt != null) {
                cdt.cancel();
            }
        }
    }
    @Override
    protected void convert(BaseViewHolder helper, ProductBean.DataBean.SceneListBean.RecordsBean item) {
        helper.setText(R.id.tv_product,item.getSceneName());

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            if (item.getEndTime()==null){
                helper.setVisible(R.id.tv_gqtime,false);
                helper.setText(R.id.tv_endtime,"有效期：");
                return;
            }else {
                helper.setVisible(R.id.tv_endtime,true);
                helper.setText(R.id.tv_endtime,"有效期："+item.getEndTime());
            }
            //1 当前时间 ，2到期时间
            Date curDate = new Date(System.currentTimeMillis());//获取当前时间
            String str = df.format(curDate);
            one = df.parse(str);
            two = df.parse(item.getEndTime());
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff ;
            if(time1 >time2) {

                helper.setText(R.id.tv_gqtime,"已过期");
            } else {
                diff = time2 - time1;
//                day = diff / (24 * 60 * 60 * 1000);
//                hour = (diff / (60 * 60 * 1000) - day * 24);
//                min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
//                sec = (diff/1000-day*24*60*60-hour*60*60-min*60);
//          if (day==0){
//              days = "";
//          }else {
//              days=day + "天";
//          }
//                helper.setText(R.id.tv_gqtime,""+days + hour + "小时" + min + "分过期" );
           //   TextView tv_gqtime = helper.getView(R.id.tv_gqtime);
                countDownTimer = new CountDownTimer(diff, 1000) {
                    public void onTick(long millisUntilFinished) {
                        day = millisUntilFinished / (24 * 60 * 60 * 1000);
                        hour = (millisUntilFinished / (60 * 60 * 1000) - day * 24);
                        min = ((millisUntilFinished / (60 * 1000)) - day * 24 * 60 - hour * 60);
                        sec = (millisUntilFinished/1000-day*24*60*60-hour*60*60-min*60);
                        String  s =(hour<10?("0"+hour):hour) + "时" + (min<10?("0"+min):min) + "分" + (sec<10?("0"+sec):sec+"秒");
                        if (day==0){
                            days=" ";
                        }else {
                            days=day+"天 ";
                        }
                        helper.setText(R.id.tv_gqtime,"剩余："+ days + s+"过期" );
                    }
                    public void onFinish() {
//                           holder.timeTv.setText("00:00:00");
//                           holder.statusTv.setText(data.name + ":结束");
                    }
                }.start();

                 countDownMap.put(helper.getView(R.id.tv_gqtime).hashCode(), countDownTimer);
            }



        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}
